package org.demo.swagger2.controller;
import	java.lang.reflect.Parameter;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.demo.swagger2.common.Result;
import org.demo.swagger2.domain.User;
import org.demo.swagger2.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class Swagger2Controller {
    @Autowired
    private IUserService userService;

    @RequestMapping(value="/list", method= RequestMethod.GET)
    public List<User> getUsers(){
        return userService.findUserList();
    }

    @ApiOperation(value="获取用户信息", notes="获取用户信息")
    @ApiImplicitParam(name="id", value="用户id",required =true, dataType ="Long", paramType = "path" )
    @RequestMapping(value="/{id}", method=RequestMethod.GET)
    public User getUserById(@PathVariable("id")long id){
        return userService.findUserById(id);
    }

    @ApiOperation(value = "删除用户信息", notes = "删除用户信息")
    @ApiImplicitParam(name="id", value="用户id",required =true, dataType ="Long", paramType = "path" )
    @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
    public Result deleteUserById(@PathVariable("id") long id){
        int i =  userService.delete(id);
        if(i == 1 ){
            return Result.success().setData(i);
        }else {
            return Result.fail();
        }
    }

    @ApiOperation(value="更新用户", notes = "更新用户")
    /*@ApiImplicitParams({
            @ApiImplicitParam(name="id", value="用户id",required =true, dataType ="Long", paramType = "path" ),
            @ApiImplicitParam(name="name", value="用户名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "age", value = "年龄" , required = true, dataType = "Integer")
    })*/
    @ApiImplicitParam(name="id", value="用户id",required =true, dataType ="Long", paramType = "path" )
    @RequestMapping(value="/{id}", method=RequestMethod.PUT)
    public Result updateUser(
                             @PathVariable("id")long id,
                             @ApiParam(name="name", value="名称", required = true, type = "String")
                             @RequestParam(value="name") String name,
                             @ApiParam(name = "age", value = "年龄" , required = true, type = "Integer")
                             @RequestParam(value="age") int age){
        User user = new User();
        user.setName(name);
        user.setAge(age);
        user.setId(id);
        int i = userService.update(user);
        if(i == 1 ){
            return Result.success().setData(i);
        }else {
            return Result.fail();
        }
    }

    @ApiOperation(value="创建用户", notes = "创建用户")
    /*@ApiImplicitParams({
            @ApiImplicitParam(name="name", value="名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "age", value = "年龄" , required = true, dataType = "Integer")
    })*/
    @RequestMapping(value="", method=RequestMethod.POST)
    public Result postUser(
            @ApiParam(name="name", value="名称", required = true, type = "String")
            @RequestParam(value="name") String name,
            @ApiParam(name = "age", value = "年龄" , required = true, type = "Integer")
            @RequestParam(value="age") int age){
        User user = new User();
        user.setName(name);
        user.setAge(age);
        int i = userService.add(user);
        if(i == 1 ){
            return Result.success().setData(i);
        }else {
            return Result.fail();
        }
    }
}
